//
//  AddStudentViewController.m
//  UI08_homework(通讯录)
//
//  Created by Hanlingyu on 16/7/25.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "AddStudentViewController.h"
#import "Person.h"

@interface AddStudentViewController ()

@end

@implementation AddStudentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *labelArray = @[@"姓名", @"性别", @"年龄", @"电话号码"];
    NSArray *textFieldArray = @[@"请添加姓名", @"请输入性别", @"请输入年龄", @"请输入电话号码"];
    
    for (int i = 0; i < 4; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        label.center = CGPointMake(60, 100 + i * 50);
        label.backgroundColor = [UIColor whiteColor];
        label.textAlignment = 1;
        label.text = labelArray[i];
        label.tag = 1000 + i;
        [self.view addSubview:label];
    }
    
    
    for (int i = 0; i < 4; i++) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        textField.center = CGPointMake(250, 100 + i * 50);
        textField.backgroundColor = [UIColor whiteColor];
        textField.placeholder = textFieldArray[i];
        textField.tag = 2000 + i;
        [self.view addSubview:textField];
    }
    self.nameTextField = [self.view viewWithTag:2000];
    self.genderTextField = [self.view viewWithTag:2001];
    self.ageTextField = [self.view viewWithTag:2002];
    self.phoneNumberTextField = [self.view viewWithTag:2003];

    
    UIButton *addStudentButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    addStudentButton.frame = CGRectMake(20, 400, self.view.frame.size.width - 40, 50);
    addStudentButton.backgroundColor = [UIColor colorWithRed:0.052 green:0.683 blue:1.000 alpha:1.000];
    addStudentButton.layer.cornerRadius = 5;
    addStudentButton.layer.masksToBounds = YES;
    [addStudentButton setTitle:@"添加联系人" forState:UIControlStateNormal];
    [addStudentButton addTarget:self action:@selector(addStudent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addStudentButton];
    
}

- (void)addStudent:(UIButton *)button {
    if ([self.nameTextField.text isEqualToString:@""] || [self.phoneNumberTextField.text isEqualToString:@""]) {
        NSLog(@"姓名和电话不能为空");
    } else {
        Person *per = [[Person alloc] initWithName:self.nameTextField.text gender:_genderTextField.text age:@(_ageTextField.text.doubleValue) phoneNumber:_phoneNumberTextField.text];
        
        
        [self.delegate sendValue:per];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
